// pages/Census/customer-list.js
const shopData = require('../../controllers/ControllerCensus').controller.shopData;
const Controllers = require('../../controllers/Controller.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // details: [
    //   {
    //     text: '昨日订单',
    //     value: 0,
    //     key: 'yesterDayNum'
    //   },
    //   {
    //     text: '本周订单',
    //     value: 0,
    //     key: 'thisWeekNum'
    //   },
    //   {
    //     text: '本月订单',
    //     value: 0,
    //     key: 'thisMonthNum'
    //   },
    //   {
    //     text: '上月订单',
    //     value: 0,
    //     key: 'lastMonthNum'
    //   },
    //   {
    //     text: '用户总数',
    //     value: 0,
    //     key: 'totalAccountNum'
    //   },
    //   {
    //     text: '新增用户',
    //     value: 0,
    //     key: 'newNum'
    //   },
    //   {
    //     text: '活动数量',
    //     value: 0,
    //     key: 'activityNum'
    //   },
    //   {
    //     text: '云商品订单',
    //     value: 0,
    //     key: 'cloudNum'
    //   },
    // ],
    page: {
      pageNumber: 1,
      pageSize: 5
    },
    list: [],
    frozenNum: null,
    normalNum: null,
    totalNum: null,
    lastPage: false,


    agentType: 0,
    tabList: [{
        text: '全部',
        state: null
      },
      {
        text: '正常',
        state: 0
      },
      {
        text: '已冻结',
        state: 1
      },
      {
        text: '待审核',
        state: 2
      },
      {
        text: '审核失败',
        state: 3
      }
    ],
    currentIndex: 0
  },
  // 代理商
  myShop() {
    var that = this;
    wx.showLoading();
    var currentIndex = that.data.currentIndex;
    var tabList = that.data.tabList;
    var state = tabList[currentIndex].state;
    var params = {};
    if (state || state == 0) params.state = state;

    Controllers.myShop(params).then(res => {
      res.data.forEach(item => {
        item.createTime = item.createTime.substr(0, 10).replace(/-/g, '.')
        item.endTime = item.endTime.substr(0, 10).replace(/-/g, '.')
        switch (item.state) {
          case 0:
            item.stateName = '正常';
            item.stateColor = 'green';
            break;
          case 1:
            item.stateName = '冻结';
            item.stateColor = 'red';
            break;
          case 2:
            item.stateName = '待审核';
            item.stateColor = 'orange';
            break;
          case 3:
            item.stateName = '审核失败';
            item.stateColor = 'red';
            break;
        }
      })
      that.setData({
        list: res.data
      });
      wx.hideLoading()
    })
  },
  // 代理商员工；
  staffMyShop() {
    var that = this;
    wx.showLoading();
    var currentIndex = that.data.currentIndex;
    var tabList = that.data.tabList;
    var state = tabList[currentIndex].state;
    var params = {};
    if (state || state == 0) params.state = state;

    Controllers.staffMyShop(params).then(res => {
      res.data.forEach(item => {
        item.createTime = item.createTime.substr(0, 10).replace(/-/g, '.')
        item.endTime = item.endTime.substr(0, 10).replace(/-/g, '.')
        switch (item.state) {
          case 0:
            item.stateName = '正常';
            item.stateColor = 'green';
            break;
          case 1:
            item.stateName = '冻结';
            item.stateColor = 'red';
            break;
          case 2:
            item.stateName = '待审核';
            item.stateColor = 'orange';
            break;
          case 3:
            item.stateName = '审核失败';
            item.stateColor = 'red';
            break;
        }
      })
      that.setData({
        list: res.data
      });
      wx.hideLoading()
    })
  },

  // 客户数量；
  agentMyShopNum() {
    Controllers.agentMyShopNum().then(res => {
      const {
        frozenNum,
        normalNum
      } = res.data
      this.setData({
        frozenNum,
        normalNum,
        totalNum: frozenNum + normalNum
      });
    })
  },

  // 客户数量(员工)；
  staffMyShopNum() {
    Controllers.staffMyShopNum().then(res => {
      const {
        frozenNum,
        normalNum
      } = res.data
      this.setData({
        frozenNum,
        normalNum,
        totalNum: frozenNum + normalNum
      });
    })
  },

  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index !== that.data.currentIndex) {
      that.setData({
        currentIndex: index
      })
      if (that.data.agentType === 1) {
        that.myShop();
      } else if (that.data.agentType === 0) {
        that.staffMyShop();
      }
    }

  },


  onShow: function () {
    // this.shopData()

    var that = this;
    app.judgeAgent(type => {
      if (type === 1) {
        that.myShop();
        that.agentMyShopNum();
      } else if (type === 0) {
        that.staffMyShop();
        that.staffMyShopNum();
      } else {
        wx.showToast({
          title: '代理商员工待接入',
          icon: 'none'
        })
      }
      that.setData({
        agentType: type
      })
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({
        url
      });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },
  // shopData() {
  //   var that = this;
  //   var page = that.data.page;
  //   shopData(page).then(res => {
  //     console.log('shopData--res==', res);
  //     const {
  //       data
  //     } = res;
  //     const {
  //       totalRow
  //     } = data.page;
  //     that.setData({
  //       totalRow,
  //       allNum: data.shopNum,
  //       lastPage: res.data.page.lastPage
  //     })
  //   })
  // },

  // getMore(){
  //   if(this.data.lastPage==false){
  //     var that = this;
  //     var page = that.data.page;
  //     page.pageNumber=page.pageNumber+1;
  //     shopData(page).then(res => {
  //       console.log('shopData--res==', res);
  //       const { code, data, desc } = res;
  //       const { totalRow } = data.page;
  //       let list2=data.page.list;
  //       let list1=this.data.list;
  //       let list3=list1.concat(list2)
  //       that.setData({
  //         page:page,
  //         list:list3,
  //         totalRow,
  //         allNum:data.shopNum,
  //         lastPage:res.data.page.lastPage
  //       })

  //     })
  //   }
  // },



  // onReachBottom: function () {
  //   this.getMore()
  // },

})